﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Register;

namespace BranchOfSansar.Register.Products
{
    public partial class frmItem : Form
    {
        private bool IsChoosing = false;
        public Item SelectedItem=null;

        public frmItem()
        {
            InitializeComponent();
            BindItem();
            IsChoosing = false;
        }
        public frmItem(bool choose):this()
        {
            IsChoosing = choose; 
        }


        private void BindItem()
        { 
            using(RegisterClient reg=new RegisterClient(Globals.Token))
            {
                gridItem.DataSource = reg.ItemList();
            }
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmRegItem frm = new frmRegItem();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BindItem(); 
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (viewItem.SelectedRowsCount < 1)
            {
                Globals.Warr("Өгөгдөл сонгогдоогүй байна!");
                return;
            }
            Item usr = (Item)viewItem.GetRow(viewItem.GetSelectedRows()[0]);
            frmRegItem frm = new frmRegItem(usr);
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                BindItem();
            }
        }

        private void frmItem_Load(object sender, EventArgs e)
        {

        }

      

        private void gridItem_DoubleClick(object sender, EventArgs e)
        {
            if (viewItem.SelectedRowsCount > 0)
            {
                SelectedItem = (Item)viewItem.GetRow(viewItem.GetSelectedRows()[0]);
                this.DialogResult = System.Windows.Forms.DialogResult.Yes;
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (viewItem.SelectedRowsCount < 1)
            {
                Globals.Warr("Өгөгдөл сонгогдоогүй байна!");
                return;
            }
            if (Globals.Question("Үйлчилгээг устгах уу?") == System.Windows.Forms.DialogResult.No)
                return;
            using (RegisterClient reg = new RegisterClient(Globals.Token))
            {
                reg.DeleteItem((Item)viewItem.GetRow(viewItem.GetSelectedRows()[0]));
            }
            BindItem();
        }
    }
}
